package com.xuyifeng.callfilter;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

public class ContactsUtils {
	static public String getContactNameFromNumber(Context context, String number) {
		// define the columns I want the query to return
		String[] projection = new String[] {
				ContactsContract.PhoneLookup.DISPLAY_NAME,
				ContactsContract.PhoneLookup.NUMBER };
	 
		// encode the phone number and build the filter URI
		Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
				 Uri.encode(number));
	 
		// query time
		Cursor c = context.getContentResolver().query(contactUri, projection, null, null, null);
	 
		// if the query returns 1 or more results
		// return the first result
		if (c.moveToFirst()) {
			String name = c.getString(c.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
			c.close();
			return name;
		}
	 
		c.close();
		// return the original number if no match was found
		return null;
	}
}
